﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DataBase_Default : BasePage
{
    BO.Documents objRegistro;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            validaSession();
            validaCohort();
            listaDocuments();
        }
    }

    private void validaCohort()
    {
        if (Session["cohortAtiva"] == null)
            Response.Redirect("prePage.aspx?redirectUrl=Documents.aspx");
    }

    private void listaDocuments()
    {
        objRegistro = new BO.Documents();
        objRegistro.objVO.IdUnidade = int.Parse(Session["cohortAtiva"].ToString());
        objRegistro.objVO.IdLevel = usuarioLogado.IdLevel;    

        rptRegistros.DataSource = objRegistro.getTodosRegistroDocumentsByUnidade();
        rptRegistros.DataBind();

        if (rptRegistros.Items.Count.Equals(0))
            lblMensagem.Text = "No results were found...";
        else
            lblMensagem.Text = string.Empty;
    }


    private static void ForceDownload(HttpResponse Response, string virtualPath, string fileName)
    {
        try
        {
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
            Response.ContentType = "application/octet-stream";
            Response.TransmitFile(virtualPath + fileName);
        }
        catch (Exception ex)
        {
            Response.Redirect("PreDocuments.aspx");
        }
    }


    protected void rptLista_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ImageButton btnDownload = (ImageButton)e.Item.FindControl("btnDownload");
        string strFileName = DataBinder.Eval(e.Item.DataItem, "Arquivo").ToString();

        if (strFileName.ToLower().Contains(".pdf"))
            btnDownload.ImageUrl = "~/DataBase/Img/img_download_pdf.jpg";
        else if (strFileName.ToLower().Contains(".doc"))
            btnDownload.ImageUrl = "~/DataBase/Img/img_download_file.jpg";
        else if (strFileName.ToLower().Contains(".txt"))
            btnDownload.ImageUrl = "~/DataBase/Img/img_download_file.jpg";
        //else
        //    btnDownload.ImageUrl = "~/Img/img_download_file.jpg";

        btnDownload.CommandArgument = strFileName;
    }

    protected void btnDownload_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btnDownload = (ImageButton)sender;

        ForceDownload(this.Response, "~/Upload/Files_Documents/", btnDownload.CommandArgument);
    }

    protected void ddlano_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}